<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples;
use App\Admin\Renderable\PaperBarChart;
use App\Admin\Widgets\Charts\Bar;
use App\Models\PaperCommit;
use App\Models\PaperFeedback;
use App\Models\PaperLibrary;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Callout;
use Dcat\Admin\Widgets\Card;

class PaperBarController extends AdminController
{
    public function index(Content $content, $id = 0)
    {
        $library = PaperLibrary::find($id);
        if ($library) {
            return $content
                ->header('《'.$library->paper_title.'》答题统计')
                ->description('')
                ->breadcrumb('答题统计')
                ->body(function (Row $row) use ($id) {
					 
                    // 获得问卷题目
                    $paperFeedbacks = PaperFeedback::where('paper_librarie_id', $id)->orderBy('subject_no', 'asc')->get();
                    // 获得所有回答
                    $paperCommits = PaperCommit::where('paper_librarie_id', $id)->get();
                    if ($paperCommits->isEmpty()) {
                        $row->column(12, Callout::make('没有用户提交调查问卷', '暂无统计数据')->removable());
                        return false;
                    }

                    // 获得答题总人数
                    $userIdArr = array_column($paperCommits->toArray(), 'user_id');
					
                    $userNum = count(array_unique($userIdArr));
				 
                    // 处理选项数据
                    $dataInfo = [];
                    foreach ($paperFeedbacks as $paperFeedbacksKey =>$paperFeedbacksVal) {
                        $tmp = [];
                        $tmp['title'] = $paperFeedbacksKey + 1 . '：' . $paperFeedbacksVal->subject_title . ($paperFeedbacksVal->is_multiple ? '（多选）' : '（单选）');
                        $tmp['sub_title'] = $userNum; // 答题总人数
                        $tmp['labels'] = []; // 选项

                        // 选项处理
                        $subjectSelect = $paperFeedbacksVal->subject_select;
                        foreach ($subjectSelect as $subjectSelectVal) {
                            $tmp['labels'][] = $subjectSelectVal['num'] . '：' . $subjectSelectVal['content'];
                            $tmp['chart_content'][$subjectSelectVal['num']] = 0; // 图表数据
                        }

                        $tmp['show_num'] = []; // 选项数据
                        $dataInfo['id' . $paperFeedbacksVal->id] = $tmp;
                    }

                    // 处理用户提交数据
                    foreach ($paperCommits as $paperCommitsVal) {
                        $answer = json_decode($paperCommitsVal->answer, true);
                        foreach ($answer as $answerVal) {
                            $dataInfo['id' . $paperCommitsVal->paper_feedback_id]['chart_content'][$answerVal] += 1;
                        }
                    }

                    // 数据计算
                    foreach ($dataInfo as &$dataInfoVal) {
                        $chartContent = [];
                        foreach ($dataInfoVal['chart_content'] as $chartContentVal) {
                            array_push($chartContent, $chartContentVal);
                            // 选项数据处理
                            $dataInfoVal['show_num'][] = round($chartContentVal / $userNum * 100) . '%（' . $chartContentVal . '）人';
                        }
                        $dataInfoVal['chart_content'] = $chartContent;

                        $row->column(6, new Examples\PaperDevices('', '', $dataInfoVal));
                    }
                });
        }


    }

}
